﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Windows.Forms.DataVisualization.Charting;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;

namespace PlanningGIS.Product
{
    public partial class FrmChart : Office2007Form
    {
        private Dictionary<string, double> data;
        private string title = "产品数据统计表";

        public FrmChart(Dictionary<string,double>source)
        {
            data = source;
            InitializeComponent();
        }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        private void FrmChart_Load(object sender, EventArgs e)
        {
            if (data != null)
            {
                chart1.Series[0].Points.Clear();
                chart1.Titles[0].Text = title;
                int count = data.Count;

                IRgbColor rgbColor = new RgbColor();
                rgbColor.RGB = ColorTranslator.ToOle(Color.Red);
                IAlgorithmicColorRamp colorRamp = new AlgorithmicColorRamp();
                colorRamp.Size = count;
                colorRamp.ToColor = rgbColor;
                rgbColor.RGB = ColorTranslator.ToOle(Color.Green);
                colorRamp.FromColor = rgbColor;
                colorRamp.Algorithm = esriColorRampAlgorithm.esriHSVAlgorithm;

                IColorRamp colorramp = (IColorRamp)colorRamp;
                colorramp.Size = count == 1 ? 2 : count;
                bool createRamp = false;
                colorramp.CreateRamp(out createRamp);
                IEnumColors enumColors = colorramp.Colors;
                enumColors.Reset();

                var dicSort = from objDic in data orderby objDic.Key descending select objDic;
                foreach (KeyValuePair<string, double> kvp in dicSort)
                {
                    DataPoint point = new DataPoint();
                    point.SetValueXY(kvp.Key, new object[] { kvp.Value.ToString() });
                    point.Label = kvp.Value.ToString();
                    point.LegendText = kvp.Key;
                    point.LabelToolTip = kvp.Key;
                    
                    IColor color = enumColors.Next();
                    point.Color = ToColor(color);
                    chart1.Series[0].Points.Add(point);
                }
                cmbType.SelectedIndex = 0;
            }
        }

        private Color ToColor(IColor color)
        {
            int R = color.RGB & 0xff;
            int G = (color.RGB & 0xff00) / 0x100;
            int B = (color.RGB & 0xff0000) / 0x10000;
            return Color.FromArgb(255, R, G, B);
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbType.SelectedItem.ToString())
            {
                case "饼状图":
                    chart1.Series[0].ChartType = SeriesChartType.Pie;
                    break;
                case "柱状图":
                    chart1.Series[0].ChartType = SeriesChartType.Column;
                    break;
                case "环形图":
                    chart1.Series[0].ChartType = SeriesChartType.Doughnut;
                    break;
                default:
                    chart1.Series[0].ChartType = SeriesChartType.Pie;
                    break;
            }
        }
    }
}
